package com.dream.dao.impl;

import com.dream.dao.UserDao;
import com.dream.pojo.User;
import com.dream.utils.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public User login(User user) {
        ArrayList<User> users = DBUtil.commonQuery(User.class,"select * from user where user_name = ? and password = ? and role = ?",
                user.getUserName(),user.getPassword(),user.getRole());
        if (users.size()>0){
            return users.get(0);
        }
        return null;
    }

    @Override
    public int add(User user) {
        try {
            DBUtil.commonUpdate("insert into user values (null,null,?,?,?,?,?,null,?,?,?)",
                    user.getUserName(),
                    user.getPassword(),
                    user.getRealName(),
                    user.getPhone(),
                    user.getRole(),
                    user.getRemark(),
                    user.getGmtCreate(),
                    user.getGmtModify()
            );
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public long totalCount(String userName) {
        String sql = "select count(user.id) FROM user where 1=1";
        StringBuilder sb = new StringBuilder(sql);
        if (userName != null && !"".equals(userName)) {
            sb.append(" and user.user_name like '%" + userName + "%'");
        }
        return DBUtil.queryCount(sb.toString());
    }

    @Override
    public List<User> list(String userName, long pageIndex, long pageSize) {
        String sql = "select * FROM user where 1=1";
        StringBuilder sb = new StringBuilder(sql);
        if (userName != null && !"".equals(userName)) {
            sb.append(" and user.user_name like '%" + userName + "%'");
        }
        sb.append(" limit ?,?");
        return DBUtil.commonQuery(User.class, sb.toString(),(pageIndex - 1) * pageSize, pageSize);
    }

    @Override
    public User findById(String userId) {
        ArrayList<User> list = DBUtil.commonQuery(User.class, "select * from user where id=?", userId);
        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public int update(User user) {
        try {
            DBUtil.commonUpdate("update user set user_name=?,password=?,real_name=?,phone=?,role=?,remark=?,gmt_modify=? where id=?",
                    user.getUserName(),
                    user.getPassword(),
                    user.getRealName(),
                    user.getPhone(),
                    user.getRole(),
                    user.getRemark(),
                    user.getGmtModify(),
                    user.getId()
            );
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int deleteById(String userId) {
        try {
            DBUtil.commonUpdate("delete from  user where id=?", userId);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
